package com.zhz.lib.cms.manage.feedback;

import java.util.ArrayList;
import java.util.List;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.zhz.lib.cms.domain.bookreview.Bookreview;
import com.zhz.lib.cms.domain.newscenter.News;
import com.zhz.lib.cms.domain.type.Sortmap;
import com.zhz.project.common.util.hibernate.BaseDAO;

public class rssFeedService {
    private BaseDAO baseDao;

    @SuppressWarnings("unchecked")
    public SyndFeed getBookreviewFeedBySortType(String sortType) {
        List<Bookreview> bookreview = baseDao
            .select("from Bookreview where subject=" + sortType+" order by bookreviewID desc");
        SyndFeed feed = new SyndFeedImpl();

        Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", sortType);

        feed.setTitle("西安交通大学图书馆 : 新书 - " + smp.getValue());
        feed.setLink("http://www.lib.xjtu.edu.cn/libCMS/rss.do?method=newsFeed&type=bookreview");
        feed.setDescription("西安交通大学图书馆新书RSS FEED!");

        feed.setEntries(getBookreviewEntriesBySortType(bookreview));
        return feed;
    }

    @SuppressWarnings("unchecked")
    private List getBookreviewEntriesBySortType(List<Bookreview> bookreview) {
        List entries = new ArrayList();
        SyndEntry entry;
        SyndContent description;

        for (Bookreview x : bookreview) {
            entry = new SyndEntryImpl();
            entry.setTitle(x.getBookname());
            entry.setLink("http://www.lib.xjtu.edu.cn/bookreview.do?event=single&id="
                          + x.getBookreviewId());

            entry.setPublishedDate(x.getPublishdate());

            description = new SyndContentImpl();
            description.setType("text/html");
            description.setValue(x.getBookname());

            entry.setDescription(description);
            entries.add(entry);
        }
        return entries;
    }

    @SuppressWarnings("unchecked")
    public SyndFeed getNewsFeedBySortType(String sortType) {
        List<News> news = baseDao.select("from News where sortType=" + sortType+" order by newsID desc");
        SyndFeed feed = new SyndFeedImpl();

        Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", sortType);

        feed.setTitle("西安交通大学图书馆 : 最新消息 - " + smp.getValue());
        feed.setLink("http://www.lib.xjtu.edu.cn/libCMS/rss.do?method=newsFeed&type=news");
        feed.setDescription("西安交通大学图书馆最新消息RSS FEED!");

        feed.setEntries(getNewsEntriesBySortType(news));
        return feed;
    }

    @SuppressWarnings("unchecked")
    private List getNewsEntriesBySortType(List<News> news) {
        List entries = new ArrayList();
        SyndEntry entry;
        SyndContent description;

        for (News x : news) {
            entry = new SyndEntryImpl();
            entry.setTitle(x.getTitle());
            entry.setLink("http://www.lib.xjtu.edu.cn/news.do?event=single&id="
                          + x.getNewsId());

            entry.setPublishedDate(x.getPubDate());

            description = new SyndContentImpl();
            description.setType("text/html");
            description.setValue(x.getTitle());

            entry.setDescription(description);
            entries.add(entry);
        }
        return entries;
    }

    public BaseDAO getBaseDao() {
        return baseDao;
    }

    public void setBaseDao(BaseDAO baseDao) {
        this.baseDao = baseDao;
    }
}